package com.framework2.utils;

import android.text.TextUtils;

import com.asiainfo.heshanghui.configs.ConstantsME;
import com.asiainfo.heshanghui.configs.EventBusTag;
import com.asiainfo.heshanghui.configs.InterfaceConfig;
import com.asiainfo.heshanghui.entity.IdsInfo;
import com.asiainfo.heshanghui.networkModel.HttpUtil;
import com.framework.Utils.PreferencesHelper;
import com.framework.Utils.ToastUtil;
import com.framework2.base.BiuooBaseActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 用于请求一些常量
 *
 * @author Yangjie
 *         className ConstantRequestUtil
 *         created at  2017/4/18  11:02
 */
public class ConstantRequestUtil {
    private static volatile ConstantRequestUtil singleton;

    private ConstantRequestUtil() {
    }

    public static ConstantRequestUtil getInstance() {
        if (singleton == null) {
            synchronized (ConstantRequestUtil.class) {
                if (singleton == null) {
                    singleton = new ConstantRequestUtil();
                }
            }
        }
        return singleton;
    }

    public void requestJustShopIds(BiuooBaseActivity activity) {
        requestJustShopIds(activity, null);
    }

    public void requestJustShopIds(BiuooBaseActivity activity, final String errorMsg) {
        HttpUtil.getInstance().requestJustShopIds(activity, InterfaceConfig.shopIds, new HttpUtil.OnRequestListResult<IdsInfo>() {
            @Override
            public void onSuccess(List<IdsInfo> list, String... msg) {
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
                if (!BiuooCollectionUtil.getInstance().isEmpty(list)) {
                    PreferencesHelper.getInstance().putInfo(ConstantsME.currentShopId, list.get(0).getShopId());
                    PreferencesHelper.getInstance().putInfo(ConstantsME.companyId, list.get(0).getCompanyId());
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < list.size(); i++) {
                        if (i == 0) {
                            stringBuilder.append(list.get(i).getShopId());
                        } else {
                            stringBuilder.append(",").append(list.get(i).getShopId());
                        }
                    }
                    PreferencesHelper.getInstance().putInfo(ConstantsME.shopIds, stringBuilder.toString());
                    EventBus.getDefault().post("Success", EventBusTag.getShopIdSuccess);
                }
            }

            @Override
            public void onFail(int code, String msg) {
                if (!TextUtils.isEmpty(errorMsg)) {
                    ToastUtil.getInstance().showToast(errorMsg);
                }
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
                if (!TextUtils.isEmpty(msg)&&msg.contains("companyId:单位ID不正确")){
                    EventBus.getDefault().post("Success", EventBusTag.noCompanyId);
                }
            }
        });
    }
}
